const CFG = require('../config');
const FUN = require('../comm/func');
const dbPcyData = require('../model/model_data');

function createTab(table, uid)
{
  let field_conf;
  let comment;

  if (table == 'dat_json')
  {
    field_conf="(\
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\
      `policy_id` int(11) unsigned NOT NULL COMMENT '所属策略',\
      `hwid` int(11) unsigned NOT NULL COMMENT '设备id',\
      `addr` int(10) unsigned NOT NULL COMMENT '从站地址',\
      `json` json NOT NULL COMMENT '寄存器或变量值',\
      `conf` json DEFAULT NULL COMMENT '数据附加配置信息',\
      `stamp` bigint(20) unsigned DEFAULT NULL COMMENT '数据产生的时间戳',\
      `time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '数据保存的时间戳',\
      PRIMARY KEY (`id`) USING BTREE,\
      KEY `FK_policy_id` (`policy_id`) USING BTREE,\
      KEY `hwid` (`hwid`) USING BTREE,\
      CONSTRAINT `FK_dat_json_"+FUN.rand(123456789,999999999)+"_"+uid+
      "` FOREIGN KEY (`policy_id`) REFERENCES `policy_id` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC";

    comment = '用户的json数据表';
  }
  // else if(table == 'dat_modbus')
  // {
  //   field_conf="(\
  //     `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\
  //     `policy_id` int(11) unsigned NOT NULL DEFAULT '4' COMMENT '所属策略',\
  //     `hwid` int(11) unsigned NOT NULL COMMENT '设备id',\
  //     `addr` int(10) unsigned NOT NULL COMMENT '从站地址',\
  //     `reg` int(10) unsigned NOT NULL COMMENT '寄存器地址',\
  //     `value` double NOT NULL DEFAULT '0' COMMENT '寄存器值',\
  //     `stamp` bigint(20) unsigned DEFAULT NULL COMMENT '数据产生的时间戳',\
  //     `time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '数据保存的时间戳',\
  //     PRIMARY KEY (`id`),\
  //     KEY `FK_policy_id` (`policy_id`),\
  //     KEY `hwid` (`hwid`),\
  //     CONSTRAINT `FK_dat_modbus_"+funcLib.rand(123456789,999999999)+"_"+uid+
  //     "` FOREIGN KEY (`policy_id`) REFERENCES `policy_id` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\
  //   ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";

  //   comment='用户的modbus数据表';
  // }
  // else if(table == 'dat_var')
  // {
  //   field_conf="(\
  //     `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\
  //     `policy_id` int(11) unsigned NOT NULL DEFAULT '4' COMMENT '所属策略',\
  //     `hwid` int(11) unsigned NOT NULL COMMENT '设备id',\
  //     `vtag` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '变量名',\
  //     `value` double NOT NULL DEFAULT '0' COMMENT '变量值',\
  //     `stamp` bigint(20) unsigned DEFAULT NULL COMMENT '数据产生的时间戳',\
  //     `time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '数据保存的时间戳',\
  //     PRIMARY KEY (`id`),\
  //     KEY `FK_policy_id` (`policy_id`),\
  //     KEY `hwid` (`hwid`),\
  //     CONSTRAINT `FK_dat_var_"+funcLib.rand(123456789,999999999)+"_"+uid+
  //     "` FOREIGN KEY (`policy_id`) REFERENCES `policy_id` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\
  //   ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";

  //   comment='用户的变量数据表';
  // }
  else if (table == 'dat_alarm')
  {
    field_conf="(\
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\
      `policy_id` int(10) unsigned NOT NULL COMMENT '策略id',\
      `hwid` varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '设备id',\
      `sn` int(10) unsigned NOT NULL COMMENT '表达式序号',\
      `msg` varchar(1000) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '报警信息',\
      `stamp` bigint(20) unsigned DEFAULT NULL COMMENT '报警产生的时间戳',\
      `time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '数据保存的时间戳',\
      PRIMARY KEY (`id`),\
      KEY `FK_policy_id` (`policy_id`),\
      KEY `hwid` (`hwid`),\
      CONSTRAINT `FK_dat_alarm_"+FUN.rand(123456789,999999999)+"_"+uid+
      "` FOREIGN KEY (`policy_id`) REFERENCES `policy_id` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";

    comment='用户的报警日志数据表';
  }
  else
  {
    console.log('not support db table:',table);
    return;
  }

  dbPcyData.createTab(uid, table, field_conf, comment).then(res=>{
    //console.log(res.warningCount);
  })
}

exports.createTab = createTab;